package listbyorder.access001_100.test50;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/1 15:27
 */
public class
Solution2 {

    // 方法二： 使用递归进行求解
    // 递归的时间复杂度狂减
    public double myPow(double x, int n) {
        if (x == -1) {
            if ((n & 1) != 0) {
                return -1;
            } else {
                return 1;
            }
        }
        if (x == 1.0) {
            return 1;
        }
        if (n == -2147483648) {
            return 0;
        }
        double mul = 1;
        if (n > 0) {
            mul = powRecursion(x, n);
            return mul;
        }
        if (n < 0) {
            n = -n;
            mul = powRecursion(x, n);
            return 1 / mul;
        }
        return mul;
    }

    private double powRecursion(double x, int n) {
        if (n == 0) {
            return 1;
        }
        double temp = powRecursion(x, n / 2);
        if ((n & 1) != 0) {
            // 如果n为奇数的话
            return temp * temp * x;
        } else {
            return temp * temp;
        }
    }
}
